package team.zero.lemon.module.sys.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.security.session.SessionContext;
import team.zero.lemon.common.service.CrudService;
import team.zero.lemon.module.sys.dao.IPortalDao;
import team.zero.lemon.module.sys.dao.IPortalUserDao;
import team.zero.lemon.module.sys.dao.IUserRoleDao;
import team.zero.lemon.module.sys.entity.Portal;
import team.zero.lemon.module.sys.entity.PortalUser;
import team.zero.lemon.module.sys.entity.UserJobOrg;

import java.util.List;

/**
 * Created by bolin on 2016/11/10.
 */
@Service
public class PortalService extends CrudService<IPortalDao, Portal> {

    @Autowired
    private IPortalUserDao iPortalUserDao;

    public List<Portal> findPortalByUserId(int userId) {
        try {
            return dao.findByUserId(userId);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    @Transactional
    public int savePortalUser(List<PortalUser> portalUsers) {
        try {
            iPortalUserDao.deleteByUserId(SessionContext.getSession().getId());
            for (PortalUser portalUser : portalUsers) {
                portalUser.setUserId(SessionContext.getSession().getId());
                portalUser.preInsert();
            }
            return iPortalUserDao.insert(portalUsers);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }
}
